package com.app.megaprosoft.modyimcarcare; 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.TabSettings;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.megaprosoft.modyimcarcare.R;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Card extends Activity {
	
	private static DecimalFormat df = new DecimalFormat("#,###,##0.00");	
	connect con = new connect();  
	ResultSet rs,rs10; 
	TextView textUser;
	TextView timeEnt;
	TextView timeComeBack; 
	String ServiceID;
	String RegisterIDT,timeHH,timeMM,sumT,dateT,timeComeBackT;
	static String ServiceIDT;
	String sumTimeT;
	String ServiceTypeT;
	String BrandT;
	String GenerationT; 
	String ColorT;
	String StartTimeT;
	String EndTimeT;
	String StatusT;
	TextView date;
	     private int mYear;
	    private int mMonth;
	    private int mDay;

		String NameInput,company;
	    
	    ArrayList<String> name = new ArrayList<String>();
	    
	    String CompanyID;
	    
		//public String filePath;

		File file;
		
		
	   String id;
	   
	   
	    
	    ArrayList<String> strSpn = new ArrayList<String>();
	    
	    	static PdfWriter writer;
			File root2 = Environment.getExternalStorageDirectory();
			File file2 = new File(root2, "getCar.pdf");
			String filePath2 = file2.getAbsolutePath();
			
			String FILE = filePath2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_card);
		Bundle bundle = getIntent().getExtras();
		ServiceID = bundle.getString("ServiceID");
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
		final Button btn1 = (Button) findViewById(R.id.button1);
		final Button btn2 = (Button) findViewById(R.id.button2);
		final Button btn3 = (Button) findViewById(R.id.next);
		textUser = (TextView) findViewById(R.id.textUser);
	
		
		con = new connect(); 
		
			
		
		String sql = "SELECT convert(varchar(50), convert(money, DataService.Sum), 1) AS Sum,  "+
		"CustomerCar.ServiceID AS ServiceID, "+
		"CustomerCar.Hour AS Hour, "+
		"CustomerCar.Minute AS Minute, "+
		"CustomerCar.RegisterID AS RegisterID, "+
		"CustomerCar.Brand AS Brand, "+
		"CustomerCar.Generation AS Generation, "+
		"CustomerCar.Color AS Color, "+
		"CustomerCar.timeComeBack AS timeComeBack, "+
		"CustomerCar.ServiceType AS ServiceType, "+
		"DataService.sumTime AS sumTime, "+
		"DataService.date AS date "+
		"FROM DataService " +

		"LEFT JOIN CustomerCar "+
		"on DataService.ServiceID = CustomerCar.ServiceID "+

		"WHERE DataService.ServiceID = '"+ServiceID+"' ";  
		
		rs = con.selectData(sql);
		try {
			rs.next();
			RegisterIDT = rs.getString("RegisterID");
			timeHH = rs.getString("Hour");
			timeMM = rs.getString("Minute");
			dateT = rs.getString("date");
			sumT = rs.getString("Sum");
			ServiceIDT = rs.getString("ServiceID");
			ServiceTypeT = rs.getString("ServiceType");
			BrandT = rs.getString("Brand");
			GenerationT = rs.getString("Generation");
			ColorT = rs.getString("Color");
			sumTimeT = rs.getString("sumTime");
			timeComeBackT = rs.getString("timeComeBack");
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace(); 
		}
		
		StartTimeT = timeHH+":"+timeMM;
		
		StatusT = "���Ѻ��ԡ��";
		
		timeToday();
		
		TextView regis = (TextView)findViewById(R.id.regis);
		TextView time = (TextView)findViewById(R.id.time);
		TextView sum = (TextView)findViewById(R.id.sum);
		date = (TextView)findViewById(R.id.date);
		timeEnt = (TextView)findViewById(R.id.timeEnd);
		TextView showid = (TextView)findViewById(R.id.textView3);
		timeComeBack = (TextView)findViewById(R.id.wait);
		
		if(timeComeBackT.equals("null")){
			timeComeBack.setText("-");
		}else{
			timeComeBack.setText(timeComeBackT);
		}
		
		
		regis.setText(RegisterIDT);
		time.setText(StartTimeT);
		timeEnt.setText(EndTimeT);
		sum.setText(sumT);
		
		
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH); 

        date.setText(mDay+"/"+(1+mMonth)+"/"+mYear);
        textUser.setText(NameInput);

	
		
		try {
			id = generateBillNo();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		showid.setText(id);
		btn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
							
				String sql2 = "INSERT INTO getCar " + 
						"(id,ServiceID,RegisterID,timeGet,Fee,Date,userGet,CreatedDate,timeComeBack) " +
						"VALUES ("+
						"'"+id+"'" + ',' + 
						"'"+ServiceIDT+"'" + ',' + 
						"'"+RegisterIDT+"'" + ',' + 
						"'"+EndTimeT+"'" + ',' +         //time get
						"'"+sumT+"'" + ',' +
						"'"+date.getText().toString()+"'" + ',' +
						"'"+textUser.getText().toString()+"'" + ',' +
				        "GETDATE() , " +
						"'"+timeComeBack.getText().toString()+"'"+ " ) "; 
				
				
				
			if(con.addData(sql2)){	 
				Toast.makeText(Card.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();
			
			}else{
				 Toast.makeText(Card.this, "Inserted data fail", Toast.LENGTH_SHORT).show();
		    
			}
			
			
	   String sqlCompany ="SELECT * FROM Company WHERE CompanyName ='"+company+"' ";  
			
			rs10 = con.selectData(sqlCompany);
			try {
				rs10.next();
				
				CompanyID = rs10.getString("OID");
				
				
			} catch (SQLException e1) {
				
				e1.printStackTrace();
			}
			
				
			
			String sql = "INSERT INTO ServiceDetail_old " + 
					"(idGetCar,ServiceID,CompanyID,ServiceType,RegisterID,Brand,Generation,Color,StartTime,EndTime,Status) " +
					"VALUES ("+
					"'"+id+"'" + ',' + 
					"'"+ServiceIDT+"'" + ',' + 
					"'"+CompanyID+"'" + ',' + 
					"'"+ServiceTypeT+"'" + ',' + 
					"'"+RegisterIDT+"'" + ',' + 
					"'"+BrandT+"'" + ',' + 
					"'"+GenerationT+"'" + ',' +
					"'"+ColorT+"'" + ',' +
					"'"+StartTimeT+"'" + ',' +
					"'"+timeEnt.getText().toString()+"'" + ',' +
			        "'"+StatusT+"'" + ") "; 
			
			  
			
			if(con.addData(sql)){	 		
				Toast.makeText(Card.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();
				pdf();
			}else{
				 Toast.makeText(Card.this, "Inserted data fail", Toast.LENGTH_SHORT).show();
		    
			}
			
			}
			
			});
			

		
		btn2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			 
				//Open Form newActivityActritis
				Intent newActivityActritis = new Intent(Card.this,MainActivity.class);
				//newActivityActritis.putExtras(newActivityActritis);
				newActivityActritis.putExtra("name", NameInput);
				newActivityActritis.putExtra("CompanyS", company);
				startActivity(newActivityActritis);
				
	}
		});
		
		
		btn3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			 
				Intent newActivityAdd = new Intent(Card.this,Detail.class);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
			    startActivity(newActivityAdd);
				

			}
			
			

			});
		
		
	}
	
	
	
	
	public String generateBillNo() throws Exception{

	      
	     String StrSql = "SELECT COUNT(id)+1," +
	     		  "MONTH(GETDATE()), "+
	    		  "CONVERT(VARCHAR(2), GETDATE(), 11) "+
                 "FROM getCar "+
                 "WHERE CONVERT(VARCHAR(7),[CreatedDate], 111) = CONVERT(VARCHAR(7), GETDATE(), 111) ";
	

				rs = con.selectData(StrSql);
				
				String No = "";
					
				if(rs.next()){
					if(rs.getInt(1) < 10)
						No = "00"+rs.getInt(1);
					else if(rs.getInt(1) < 100)
						No = "0"+rs.getInt(1);
					else
						No = ""+rs.getInt(1);
				
			
					return ""+rs.getInt(3)+""+rs.getInt(2)+No;	// Prefix + YYMMDD + XXXX
					
				}else{
					
					return null;
					//return null;
				}
}
	 


	public void timeToday(){
		

		// TODO Auto-generated method stub

		int  sum = Integer.parseInt(sumTimeT);
		 
		int  Result = sum+Integer.parseInt(timeMM); 
		 
		 
		// t2.setText(HH+":"+MM+"�.");
		
		if(120>Result&&Result>=60){
			int s1 = Result-60;
			int hr = Integer.parseInt(timeHH)+1;
		
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT = hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}
		}
		
	else if(180>Result&&Result>=120){

			int  s1 = Result-120;
			int  hr = Integer.parseInt(timeHH)+2;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)
				EndTimeT = hr+":0"+s1;
			else{
			
				EndTimeT = hr+":"+s1;
			}
		}
		else if(240>Result&&Result>=180){
			 int s1 = Result-180;
			 int hr = Integer.parseInt(timeHH)+3;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
			
		}	
		else if(300>Result&&Result>=240){
			int s1 = Result-240;
			 int hr = Integer.parseInt(timeHH)+4;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}	
		else if(360>Result&&Result>=300){
			int s1 = Result-300;
			 int hr = Integer.parseInt(timeHH)+5;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}	
		else if(420>Result&&Result>=360){
			int s1 = Result-360;
			 int hr = Integer.parseInt(timeHH)+6;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}	
		else if(480>Result&&Result>=420){
			int s1 = Result-420;
			 int hr = Integer.parseInt(timeHH)+7;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(540>Result&&Result>=480){
			int s1 = Result-480;
			 int hr = Integer.parseInt(timeHH)+8;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(600>Result&&Result>=540){
			int s1 = Result-540;
			 int hr = Integer.parseInt(timeHH)+9;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(660>Result&&Result>=600){
			int s1 = Result-600;
			 int hr = Integer.parseInt(timeHH)+10;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(720>Result&&Result>=660){
			int s1 = Result-660;
			 int hr = Integer.parseInt(timeHH)+11;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(780>Result&&Result>=720){
			int s1 = Result-720;
			 int hr = Integer.parseInt(timeHH)+12;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(840>Result&&Result>=780){
			int s1 = Result-780;
			 int hr = Integer.parseInt(timeHH)+13;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(900>Result&&Result>=840){
			int s1 = Result-840;
			 int hr = Integer.parseInt(timeHH)+14;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(960>Result&&Result>=900){
			int s1 = Result-900;
			 int hr = Integer.parseInt(timeHH)+15;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1020>Result&&Result>=960){
			int s1 = Result-960;
			 int hr = Integer.parseInt(timeHH)+16;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1080>Result&&Result>=1020){
			int s1 = Result-1020;
			 int hr = Integer.parseInt(timeHH)+17;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1140>Result&&Result>=1080){
			int s1 = Result-1080;
			 int hr = Integer.parseInt(timeHH)+18;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1200>Result&&Result>=1140){
			int s1 = Result-1140;
			 int hr = Integer.parseInt(timeHH)+19;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1260>Result&&Result>=1200){
			int s1 = Result-1200;
			 int hr = Integer.parseInt(timeHH)+20;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1320>Result&&Result>=1260){
			int s1 = Result-1260;
			 int hr = Integer.parseInt(timeHH)+21;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1380>Result&&Result>=1320){
			int s1 = Result-1320;
			 int hr = Integer.parseInt(timeHH)+22;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else if(1440>Result&&Result>=1380){
			int s1 = Result-1380;
			 int hr = Integer.parseInt(timeHH)+23;
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
		}
		else{
			
			int s1 = Result;
			 int hr = Integer.parseInt(timeHH); 
			if(s1==0||s1==1||s1==2||s1==3||s1==4||s1==5||s1==6||s1==7||s1==8||s1==9)						
				EndTimeT =hr+":0"+s1;						
			else{
				
				EndTimeT = hr+":"+s1;
			}	
			
		}	
	}




@Override
public void onBackPressed() {
  
        new AlertDialog.Builder(this)
        .setTitle("¡��ԡ��÷���¡��")
        .setMessage("��Ѻ�˹����ѡ")
                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent newActivity = new Intent(getApplicationContext(),MainActivity.class);	
                        newActivity.putExtra("name", NameInput);
                        newActivity.putExtra("CompanyS", company);
                        startActivity(newActivity);
                        finish();
                        overridePendingTransition(R.layout.fadeendout,R.layout.fadeendin);
                    }
                })
                .setPositiveButton(android.R.string.no, null).create().show();
    

}


			//===================================================PRIN====================================================

public void print(){
	
	Uri uri = Uri.fromFile(file2);
	Intent intent = new Intent(Intent.ACTION_SEND);
	String mimeType = MimeTypeMap.getSingleton()
	        .getMimeTypeFromExtension(MimeTypeMap
	                .getFileExtensionFromUrl(filePath2));
	intent.setType(mimeType);
	intent.putExtra(Intent.EXTRA_STREAM, uri );
	startActivity(Intent.createChooser(intent, "Share file via"));

}


			
public void pdf(){
	 

    
	   try {
		      Document document = new Document();
		      Rectangle pagesize = new Rectangle(190f, 720f);
		      document.setPageSize(pagesize);
		      document.setMargins(0, 0, 0, 0);
	
		     writer = PdfWriter.getInstance(document, new FileOutputStream(FILE));
		      document.open();
		    
		      addMetaData(document);
		  
		    //  addTitlePage(document);
		      addContent(document);
		      document.close();
		      print();
		      Toast.makeText(Card.this, "��¹", Toast.LENGTH_SHORT).show();
		    
		    } catch (Exception e) {
		      e.printStackTrace();
		      Toast.makeText(Card.this, "�����¹", Toast.LENGTH_SHORT).show();
		    }
	   
 }

	
	
	
// iText allows to add metadata to the PDF which can be viewed in your Adobe
// Reader
// under File -> Properties
private static void addMetaData(Document document) {
 document.addTitle("Get Care");
 document.addSubject("Using iText");
 document.addKeywords("Java, PDF, iText");
 document.addAuthor("Get Care");
 document.addCreator("Get Care");
}


private static void addContent(Document document) throws DocumentException, IOException {
	  
	  connect con = new connect();

		File path = new File(Environment.getExternalStorageDirectory(), "cordia.ttf");
		String a = path.getAbsolutePath();
		System.out.println(a);
	  
	
	 BaseFont font = BaseFont.createFont(a, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	 Font title = new Font(font, 10, Font.BOLD);	
	 Font content = new Font(font, 8, Font.NORMAL);	

	
	 
	 

		String sql = "SELECT getCar.id AS id " +
			      ",getCar.ServiceID AS ServiceID " +
			      ",getCar.RegisterID AS RegisterID " +
			      ",getCar.timeGet AS timeGet " +
			      ",getCar.Fee AS Fee " +
			      ",getCar.Date AS Date " +
			      ",getCar.userGet AS userGet " +
			      ",getCar.timeComeBack AS timeComeBack " +
			      ",getCar.CreatedDate AS CreatedDate " +
			      ",CustomerCar.Hour + ':' + CustomerCar.Minute AS timein " +
			      
			      "FROM getCar " +
			  
			      "LEFT JOIN CustomerCar " +
			      "on getCar.ServiceID = CustomerCar.ServiceID " +
						    
			      "WHERE getCar.ServiceID = '"+ServiceIDT+"' ";
		
						ResultSet rs;  
						
						rs = con.selectData(sql);
						
						ArrayList<HashMap<String, String>> data = null;
						data = new ArrayList<HashMap<String,String>>();
						try {
							while(rs.next()) {
						HashMap<String, String>  datanum = new HashMap<String, String>();
						datanum.put("id", rs.getString("id"));
						datanum.put("ServiceID", rs.getString("ServiceID"));
						datanum.put("RegisterID", rs.getString("RegisterID"));
						datanum.put("timeGet", rs.getString("timeGet"));
						datanum.put("Fee", rs.getString("Fee"));
						datanum.put("Date", rs.getString("Date"));
						datanum.put("userGet", rs.getString("userGet"));
						datanum.put("timein", rs.getString("timein"));	
						datanum.put("timeComeBack", rs.getString("timeComeBack"));	
						data.add(datanum); 
					
							}
						} catch (SQLException e) {
							//TODO Auto-generated catch block
							e.printStackTrace();
						//	System.out.println("NO");
						}


									 

	 Paragraph prefaceh = new Paragraph(); 
  prefaceh.add(new Phrase("��Ѻö",title));
  prefaceh.setAlignment(Element.ALIGN_CENTER);
  addEmptyLine(prefaceh, 1);
  document.add(prefaceh);
  
  Paragraph preface = new Paragraph(); 
  preface.add(new Phrase("�ٹ���������ö����ѹ",content));
  preface.setAlignment(Element.ALIGN_CENTER);
  document.add(preface);
  
//==============================================HEAD TITLE===================================================   
  Paragraph subTitle = new Paragraph(); 
  
  PdfPTable table1 = new PdfPTable(2);
  PdfPTable table = new PdfPTable(2);
  
  float[] setsize1 = new float[]{40,60};   
  table1.setWidths(setsize1);
  
   table1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
  table1.getDefaultCell().setBorder(0);
   table1.addCell("");
  
   table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
  table.getDefaultCell().setBorder(0);
  

  table.addCell(new Phrase("�Ţ�����Ѻö",content));
  table.addCell(new Phrase(":   "+data.get(0).get("id").toString(),content));
  table.addCell(new Phrase("�ѹ���",content));
  table.addCell(new Phrase(":   "+data.get(0).get("Date").toString(),content));
  table.addCell(new Phrase("�Ţ�����Ѻ��ԡ��",content));
  table.addCell(new Phrase(":   "+data.get(0).get("ServiceID").toString(),content));


  table1.addCell(table);

  subTitle.add(table1);
  subTitle.setAlignment(Element.ALIGN_RIGHT);
  addEmptyLine(subTitle, 1);
  document.add(subTitle); 
  
  subTitle = new Paragraph(new Phrase("       ��������´",content));
  addEmptyLine(subTitle, 2);
  document.add(subTitle); 
  
 
//==============================================DETAIL===================================================    
  Paragraph carDetail = new Paragraph(); 
  
  PdfPTable datatable1 = new PdfPTable(2);
  PdfPTable subtable = new PdfPTable(2);

   
  float[] datatable1Size = new float[]{10,90};  
  float[] widths = new float[]{45,55};   
  datatable1.setWidths(datatable1Size);
  subtable.setWidths(widths);

  
  
    datatable1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
datatable1.getDefaultCell().setBorder(0);
    datatable1.addCell("");
  
//Sub table Table 1
  subtable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
subtable.getDefaultCell().setBorder(0);
  

  subtable.addCell(new Phrase("����¹ö",content));
  subtable.addCell(new Phrase(data.get(0).get("RegisterID").toString(),content));
  subtable.addCell(new Phrase("�����Ѻ��ԡ��",content));
  subtable.addCell(new Phrase(data.get(0).get("timein").toString()+"  �.",content));
  subtable.addCell(new Phrase("���ҹѴ����",content));
  subtable.addCell(new Phrase(data.get(0).get("timeGet").toString()+"  �.",content));
  subtable.addCell(new Phrase("���ҹѴ���¡ó��١��� ������Ѻö",content));
  subtable.addCell(new Phrase(data.get(0).get("timeComeBack").toString()+"  �.",content));
  subtable.addCell(new Phrase("��Һ�ԡ���»���ҳ",content));
  subtable.addCell(new Phrase( data.get(0).get("Fee").toString()+"  �ҷ",content));
  subtable.addCell(new Phrase("��ѡ�ҹ�Ѻ��ԡ��",content));
  subtable.addCell(new Phrase(data.get(0).get("userGet").toString(),content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase("____________________",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase("         �١���                                ",content));
  subtable.addCell(new Phrase(" ",content));
  subtable.addCell(new Phrase(" ",content));

//add table 1 
  datatable1.addCell(subtable);

  
  carDetail.add(datatable1);
  addEmptyLine(carDetail, 1);
  document.add(carDetail); 
  

  Paragraph footDetail = new Paragraph(); 
  PdfPTable foottable = new PdfPTable(1);
  footDetail.setFont(content);
  footDetail.setLeading(0f, 1.0f); 
  footDetail.add("     ��������觢ͧ�դ������ö �ҧ�ٹ��� ������Ѻ�Դ�ͺ�ء�ó�  ��س��ʴ���Ѻö������͵Դ��͢��Ѻö");
  footDetail.add( Chunk.NEWLINE );
  footDetail.add("     �Ң���ԭ���ͧ  053-302953 �Ң��Ѳ�� 053-271079 �Ң���ࢵ 053-242175 �Ң�ʹ���Թ 053-3806840");
  footDetail.setAlignment(Element.ALIGN_LEFT);
  foottable.addCell(footDetail);
  document.add(foottable);
  
 

}

private static void addEmptyLine(Paragraph paragraph, int number) {
 for (int i = 0; i < number; i++) {
   paragraph.add(new Paragraph(""));
 }
}





public String generationOID() throws Exception{

	  
    String StrSql = "SELECT COUNT(OID)+1," +
    		  "DAY(GETDATE()), "+
    		  "MONTH(GETDATE()), "+
    		  "CONVERT(VARCHAR(2)," +
    		  "GETDATE(), 11) "+
            "FROM CarGenerations "+
            "WHERE CONVERT(VARCHAR,[CreatedDate], 111) = CONVERT(VARCHAR, GETDATE(), 111) ";
    /* 
	
	 String StrSql = "SELECT COUNT(OID)+1 FROM Colors " +
	 		"WHERE CAST(FLOOR(CAST([CreatedDate] AS FLOAT)) AS DATETIME) = CONVERT(varchar,GETDATE(), 111)";
*/
			rs = con.selectData(StrSql);
			
			String No = "";
			String day = "";
				
			if(rs.next()){
				if(rs.getInt(1) < 10){
					No = "000"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
				}
				else if(rs.getInt(1) < 100){
					No = "00"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else if(rs.getInt(1) < 1000){
					No = "0"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else{
					No = ""+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
			
		
				return "10020"+rs.getInt(4)+rs.getInt(3)+""+day+""+No;	// Prefix + YYMMDD + XXXX
				
				
				
				
			}else{
				
				return null;
				//return null;
			}
}

//=======================================================================================================
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.card, menu);
		return true;
	}

}
